﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Caching;
using System.Xml;

namespace __Ts.Core.Configuration
{
    /// <summary>
    /// code by able 2011.03.22
    /// </summary>
    public class ConfigManager : Dictionary<string, NameValueCollection>
    {
        /// <summary>
        /// 
        /// </summary>
        private static ConfigManager _con;
        /// <summary>
        /// 
        /// </summary>
        private ConfigManager() { }
        /// <summary>
        /// 缓存Key
        /// </summary>
        private static string CACHE_CONFIG_XML = "CACHE:CONFIG_XML_";
        /// <summary>
        /// 
        /// </summary>
        public static ConfigManager AppSettings
        {
            get
            {
                if (_con == null)
                    _con = new ConfigManager();
                return _con;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public NameValueCollection this[int port]
        {
            get
            {
                string cacheKey = CACHE_CONFIG_XML + port;
                string xmlPath = string.Format(@"{0}\Configs\{1}.xml", Utilities.Util.RootPath, port);

                NameValueCollection ht = new NameValueCollection();
                if (HttpRuntime.Cache[cacheKey] != null)
                    ht = HttpRuntime.Cache[cacheKey] as NameValueCollection;
                else
                {
                    if (File.Exists(xmlPath))
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.Load(xmlPath);
                        XmlNode node = doc.SelectSingleNode("configuration");
                        for (int i = 0; i < node.ChildNodes.Count; i++)
                        {
                            if (node.ChildNodes[i].GetType() != typeof(XmlComment))
                                ht.Add(node.ChildNodes[i].Name, node.ChildNodes[i].InnerText);
                        }
                        HttpRuntime.Cache.Add(cacheKey, ht, new CacheDependency(xmlPath), DateTime.Now.AddMonths(1), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
                    }
                }
                return ht;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static NameValueCollection GlobalAppSettings
        {
            get
            {
                NameValueCollection ht = new NameValueCollection();
                if (HttpRuntime.Cache[CACHE_CONFIG_XML] != null)
                    ht = HttpRuntime.Cache[CACHE_CONFIG_XML] as NameValueCollection;
                else
                {
                    string xmlPath = string.Format(@"{0}\config.xml", Utilities.Util.RootPath);
                    if (File.Exists(xmlPath))
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.Load(xmlPath);
                        XmlNode node = doc.SelectSingleNode("configuration");
                        for (int i = 0; i < node.ChildNodes.Count; i++)
                        {
                            if (node.ChildNodes[i].GetType() != typeof(XmlComment))
                                ht.Add(node.ChildNodes[i].Name, node.ChildNodes[i].InnerText);
                        }
                        HttpRuntime.Cache.Add(CACHE_CONFIG_XML, ht, new CacheDependency(xmlPath), DateTime.Now.AddMonths(1), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
                    }
                    else
                    {
                        ht = ConfigurationManager.AppSettings;
                    }
                }
                return ht;
            }
        }
    }
}
